%%%---------------------------------------------------------------------------------------
%%% @author     Stuart Jackson <simpleenigma@gmail.com> [http://erlsoft.org]
%%% @copyright  2006 - 2007 Simple Enigma, Inc. All Rights Reserved.
%%% @doc        ERML references and code
%%% @reference  See <a href="http://erlsoft.org/modules/erlweb" target="_top">Erlang Software Framework</a> for more information
%%% @reference See <a href="http://erlweb.googlecode.com" target="_top">ErlWeb Google Code Repository</a> for more information
%%% @version    0.0.2
%%% @since      0.0.1
%%% @end
%%%
%%%
%%% The MIT License
%%%
%%% Copyright (c) 2007 Stuart Jackson, Simple Enigma, Inc. All Righs Reserved
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in
%%% all copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%%% THE SOFTWARE.
%%%
%%%
%%%---------------------------------------------------------------------------------------
-module(erml).
-author('sjackson@simpleenigma.com').
-include("../include/erml.hrl").
-include_lib("xmerl/include/xmerl.hrl").

-export([scan/1,process/2,process_string/2]).

%%-------------------------------------------------------------------------
%% @spec (FileName::string()) -> {ok,erml_processed,XHTML::xml(),ERML::erml_record()} | {ok,no_erml_commands}
%% @doc  Reads in a file name and processes all ERML commands
%% @end
%%-------------------------------------------------------------------------

scan(FileName) when is_list(FileName) ->
	scan(#erml{filename=FileName});
scan(ERML) when is_record(ERML,erml) ->
	case erml_cmd:check_erml(ERML#erml.filename) of
		false -> {ok,no_erml_commands};
		{true,String} ->
			{XMLDoc,_Misc} = xmerl_scan:string(String),
			%% Process the XMLDoc and modify it based on ERML commands.
			io:format("Processing ERML Commands~n"),
			{NewXMLDoc,FinalERML} = process(XMLDoc,ERML),
			XHTML = lists:flatten(xmerl:export([NewXMLDoc],xmerl_html)),
			Clean_XHTML = erml_cmd:clean_erml(XHTML),
			{ok,erml_processed,Clean_XHTML,FinalERML}
	end.

%%-------------------------------------------------------------------------
%% @spec (XMLDoc::xml(),ERML::erml_record()) -> {XHTML::xml(),ERML::erml_record()}
%% @doc  Takes XML and processes ERML commands.
%% @end
%%-------------------------------------------------------------------------
process(XMLDoc,ERML) -> 
	{NewXMLDoc,FinalERML} = xmerl_lib:mapfoldxml(fun(E,Orig_ERML) -> 
		case erml_cmd:name_prefix(E) of
			"erml" -> erml_cmd:command(E,Orig_ERML); 
			_Other -> 
				case erml_cmd:contains_variable(E) of
					true -> 
						NewE = erml_cmd:var(E,Orig_ERML),
						{NewE,Orig_ERML}; 
					_ -> {E,Orig_ERML}
				end
		end
	end,ERML,XMLDoc),
	{NewXMLDoc,FinalERML}.

%%-------------------------------------------------------------------------
%% @spec (XMLDoc::xml(),ERML::erml_record()) -> {XHTML::xml(),ERML::erml_record()}
%% @doc  Takes string and processes ERML commands.
%% @end
%%-------------------------------------------------------------------------
process_string(String,ERML) ->
	{XMLDoc,_Misc} = xmerl_scan:string("<ermloutput>" ++ String ++ "</ermloutput>"),
	{NewElement,_FinalERML} = erml:process(XMLDoc,ERML),
	{NewElement,ERML}.